#include "Trigger.h"

using namespace DAISY;


Trigger::Trigger(int id):_id(id),_isEnable(false)
{}
Trigger::~Trigger(void)
{
	clearEventHandler();
}


void Trigger::addEventHandler(TriggerEventHandlerPtr eh, EventManager * manager)
{
	eh->setCallback(this);
	_eventHandlers.push_back(std::make_pair(eh, manager));
	if(_isEnable)
		manager->comeIn(eh);
}
void Trigger::addAction(TriggerActionPtr action)
{
	_actions.push_back(action);
}


void Trigger::addConditions(TriggerConditionsPtr conditions)
{
	_conditionses.push_back(conditions);
}
void Trigger::clearEventHandler(void)
{
	_stop();
	_eventHandlers.clear();
}


void Trigger::execute(Event * evt)
{
	BOOST_FOREACH(TriggerConditionsPtr conditions, _conditionses)
	{
		if(!(*conditions)(evt))
			return;
	}
	_action(evt);
	disable();
}

void Trigger::_start(void)
{
	BOOST_FOREACH(EventHandlerPair eh, _eventHandlers)
	{
		eh.second->comeIn(eh.first);
	}
}
void Trigger::_stop(void)
{
	BOOST_FOREACH(EventHandlerPair eh, _eventHandlers)
	{
		eh.second->goOut(eh.first);
	}
}
void Trigger::_action(Event * evt)
{
	BOOST_FOREACH(TriggerActionPtr action, _actions)
	{
		(*action)(_id, evt);
	}

}

void Trigger::enable(void)
{
	_isEnable = true;
	_start();
}
void Trigger::disable(void)
{
	_stop();
	_isEnable = false;
}